<template>
  <div class="auth-container">
    <div class="auth-box">
      <h2 class="auth-title">登录</h2>
      <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item>
          <div class="button-group">
            <el-button type="primary" @click="handleLogin" :loading="loading">
              登录
            </el-button>
            <el-button type="info" @click="resetForm">
              重置
            </el-button>
          </div>
        </el-form-item>
        <div class="text-center">
          <el-button link @click="$router.push('/register')">没有账号？立即注册</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { auth } from '@/api'

const router = useRouter()
const formRef = ref(null)
const loading = ref(false)

const form = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const res = await auth.login(form)
        localStorage.setItem('token', res.token)
        localStorage.setItem('username', res.username)
        localStorage.setItem('role', res.role)
        ElMessage.success('登录成功')
        router.push('/')
      } catch (error) {
        console.error('登录错误详情:', error)
        if (error.response) {
          console.error('响应状态:', error.response.status)
          console.error('响应数据:', error.response.data)
          console.error('响应头:', error.response.headers)
          
          if (error.response.status === 403) {
            ElMessage.error('登录被拒绝，请检查后端服务是否正常运行')
          } else if (error.response.status === 401) {
            ElMessage.error('用户名或密码错误')
          } else {
            ElMessage.error('登录失败: ' + (error.response.data?.message || error.message))
          }
        } else if (error.request) {
          console.error('请求未收到响应:', error.request)
          ElMessage.error('无法连接到服务器，请检查后端服务是否启动')
        } else {
          console.error('请求配置错误:', error.message)
          ElMessage.error('请求配置错误: ' + error.message)
        }
      } finally {
        loading.value = false
      }
    }
  })
}

const resetForm = () => {
  if (formRef.value) {
    formRef.value.resetFields()
  }
}
</script>

<style scoped>
.auth-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(120deg, #a1c4fd, #c2e9fb);
  font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;
}

.auth-box {
  width: 100%;
  max-width: 420px;
  padding: 35px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 12px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;
}

.auth-box:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
}

.auth-title {
  text-align: center;
  margin-bottom: 35px;
  color: #409EFF;
  font-size: 28px;
  font-weight: 600;
  letter-spacing: 1px;
}

.text-center {
  text-align: center;
  margin-top: 20px;
}

.button-group {
  display: flex;
  gap: 15px;
}

.button-group .el-button {
  flex: 1;
}

:deep(.el-input__inner) {
  border-radius: 8px;
  height: 45px;
  transition: all 0.3s;
}

:deep(.el-input__inner:focus) {
  border-color: #409EFF;
  box-shadow: 0 0 5px rgba(64, 158, 255, 0.5);
}

:deep(.el-button--primary) {
  height: 45px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;
  background: linear-gradient(to right, #409EFF, #36D1DC);
  border: none;
  transition: all 0.3s;
}

:deep(.el-button--primary:hover) {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(64, 158, 255, 0.4);
}

:deep(.el-button--info) {
  height: 45px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;
  background: linear-gradient(to right, #909399, #C0C4CC);
  border: none;
  transition: all 0.3s;
}

:deep(.el-button--info:hover) {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(144, 147, 153, 0.4);
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #606266;
}
</style> 